﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;

namespace FBProject.BusinessLogic
{
    public static class ApplicationConfiguration
    {
        private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(ApplicationConfiguration));

        private static string GetSetting(string settingName)
        {
            return GetSetting(settingName, string.Empty);
        }

        private static string GetSetting(string settingName, string defaultValue)
        {
            Configuration configurationFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            AppSettingsSection appSettingSection = configurationFile.AppSettings;
            var setting = appSettingSection.Settings[settingName];
            if (setting != null)
            {
                return appSettingSection.Settings[settingName].Value;
            }
            else
            {
                return defaultValue;
            }
        }

        private static void SaveSetting(string settingName, string settingValue)
        {
            Configuration configurationFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            AppSettingsSection appSettingSection = configurationFile.AppSettings;
            appSettingSection.Settings.Remove(settingName);
            appSettingSection.Settings.Add(settingName, settingValue);
            configurationFile.Save(ConfigurationSaveMode.Modified);
        }

        public static string FBFilePattern
        { 
            get { return GetSetting("FBFilePattern", "Filter*.csv"); }
            set { SaveSetting("FBFilePattern", value); }
        }

        public static string FBFilePath
        {
            get
            {
                string fbFilePath = GetSetting("FBFilePath");
                if (string.IsNullOrEmpty(fbFilePath))
                {
                    fbFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Downloads");
                    if (!Directory.Exists(fbFilePath))
                    {
                        Logger.InfoFormat("FBFilePath - Path {0} doesn't exist", fbFilePath);
                        fbFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads");
                        if (!Directory.Exists(fbFilePath))
                        {
                            Logger.InfoFormat("FBFilePath - Path {0} doesn't exist", fbFilePath);
                            fbFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                        }
                    }
                    Logger.InfoFormat("FBFilePath - Setting default value to {0}", fbFilePath);
                }

                return fbFilePath;
            }
            set
            {
                SaveSetting("FBFilePath", value);
            }
        }

        public static bool AdvancedUI
        {
            get { return (string.CompareOrdinal(GetSetting("AdvancedUI"), "true") == 0); }
            set { SaveSetting("AdvancedUI", value.ToString()); }
        }

        public static string LastMSProject
        {
            get { return GetSetting("LastMSProject"); }
            set { SaveSetting("LastMSProject", value); }
        }

    }
}
